//
//  TFHorizontalMoreView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/2/16.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFHorizontalMoreView: UIView {
    
    let view = UIButton()
    
    @objc public let titleLab = UILabel()
    @objc public var closeBtn = UIButton()
    /**公告*/
    @objc public let gonggaoBtn = TFMoreViewButton()
    /**提问记录*/
    @objc public let askRecordBtn = TFMoreViewButton()

    /**网络诊断*/
    @objc public let netWorkBtn = TFMoreViewButton()
    /**修改名称*/
    @objc public let changeNickNameBtn = TFMoreViewButton()
    /**课件下载*/
    @objc public let courseDownloadBtn = TFMoreViewButton()
    
    @objc func viewClick()
    {
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.isHidden = true
        
        self.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 0.5)
        self.view.addTarget(self, action: #selector(viewClick), for: .touchUpInside)
        self.addSubview(self.view)
        self.view.backgroundColor = UIColor.white
        
        self.titleLab.text = "更多"
        self.titleLab.textColor = UIColor(red: 48/255.0, green: 51/255.0, blue: 75/255.0, alpha: 1.0)
        
        self.titleLab.textAlignment = .center
        self.titleLab.font = UIFont.PingFang(size: 18)
        self.view.addSubview(self.titleLab)
    
        self.closeBtn = UIButton.init()
        self.closeBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
        self.view.addSubview(self.closeBtn)
        self.closeBtn.addTarget(self, action: #selector(self.closeHormoreBtnClick), for: .touchUpInside)
        
        
        self.gonggaoBtn.img.image = UIImage.init(named: "HTLive.bundle/asklist_more")
        self.gonggaoBtn.lab.text = "公告";
        self.view.addSubview(self.gonggaoBtn)
        
        self.askRecordBtn.img.image = UIImage.init(named: "HTLive.bundle/asklist")
        self.askRecordBtn.lab.text = "提问记录";
        self.view.addSubview(self.askRecordBtn)
        
        self.netWorkBtn.img.image = UIImage.init(named: "HTLive.bundle/net_more")
        self.netWorkBtn.lab.text = "网络选择";
        self.view.addSubview(self.netWorkBtn)
        
        
        self.courseDownloadBtn.img.image = UIImage.init(named: "HTLive.bundle/kejianDown")
        self.courseDownloadBtn.lab.text = "课件下载";
        self.view.addSubview(self.courseDownloadBtn)

     
    }
    
    // #pragma mark  横屏 ====================
    @objc func closeHormoreBtnClick()
    {
        self.showView(false)
    }
    
    func showView(_ isShow:Bool)
    {
        
        
        let viewW:CGFloat = 375
        var viewX = self.frame.width
        
        if isShow {
            viewX = self.frame.width - viewW
        }
        
        if isShow {
            self.view.frame = CGRect(x: self.frame.width, y: 0, width: 375, height: self.frame.height)
        }
        
        if (self.superview != nil) {
            self.superview?.addSubview(self)
        }
        self.isHidden = false
        UIView.animate(withDuration: 0.5, delay: 0.0, options: UIView.AnimationOptions.allowAnimatedContent) {
            self.view.frame = CGRect(x: viewX, y: 0, width: viewW, height: self.frame.height)
        } completion: { Bool in
            
            if isShow == false {
                self.isHidden = true
            }
        }
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
       
        
        let sizeWidth = self.view.frame.size.width
        
        self.titleLab.frame = CGRect(x: (sizeWidth-100)/2.0, y: 16, width: 100, height: 25)
        self.closeBtn.frame = CGRect(x: sizeWidth-30-12, y: 13, width: 30, height: 30)
        self.gonggaoBtn.frame = CGRect(x:0, y: self.titleLab.frame.maxY, width: sizeWidth/4.0, height: 60)
        self.askRecordBtn.frame = CGRect(x: self.gonggaoBtn.frame.maxX, y: self.titleLab.frame.maxY, width: sizeWidth/4.0, height: 60)
        self.netWorkBtn.frame = CGRect(x: self.askRecordBtn.frame.maxX, y: self.titleLab.frame.maxY, width: sizeWidth/4.0, height: 60)
        if(self.netWorkBtn.isHidden)
        {
            self.courseDownloadBtn.frame = self.netWorkBtn.frame
        }else
        {
            self.courseDownloadBtn.frame = CGRect(x: self.netWorkBtn.frame.maxX, y: self.titleLab.frame.maxY, width: sizeWidth / 4.0, height: 60)
        }
    
        
    }
    
    @objc public func courseDown(_ isShow:Bool)
    {
        self.courseDownloadBtn.isHidden = !isShow;
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.showView(false)
    }
}
